home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / SHAPES.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  4KB  |  145 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef SGI
  4. #include "gl.h"
  5. #include "device.h"
  6. #else
  7. #include "vogl.h"
  8. #include "vodevice.h"
  9. #endif
  10.  
  11. /*
  12.  * This program shows some of the simple primitives.
  13.  */
  14. main()
  15. {
  16.     Screencoord    minx, maxx, miny, maxy, edgelength;
  17.     short        val;
  18.  
  19.     winopen("shapes");
  20.  
  21.     /*
  22.      * the two lines below clear the screen to white if we have
  23.      * colours, on a monochrome device color is ignored so the
  24.      * screen will be cleared to its background color, normally black.
  25.      */
  26.     color(BLACK);
  27.     clear();
  28.  
  29.     /*
  30.      * set the screen to be 2.0 units wide and 2.0 units wide, with
  31.      * the drawable coordinates going from -1.0 to 1.0.
  32.      */
  33.     ortho2(-1.0, 1.0, -1.0, 1.0);
  34.  
  35.     color(MAGENTA);
  36.  
  37.     /*
  38.      * okay, so we want to draw in the range -1 to 1, but we
  39.      * only want to draw in the top lefthand corner of the
  40.      * screen. The call to viewport allows us to do this. As
  41.      * viewport always takes screen coordinates, we need to
  42.      * call getviewport to found out how big our screen is
  43.      * at the moment. We use the values returned from getviewport
  44.      * calculate the positions for our new viewport. We note
  45.      * that on an Iris (0,0) is the bottom left pixel.
  46.      */
  47.     getviewport(&minx, &maxx, &miny, &maxy);
  48.  
  49.     viewport(minx, (maxx - minx) / 2, (maxy - miny) / 2, maxy);
  50.  
  51.     cmov2(-0.9, -0.5);        /* write out a heading */
  52.     charstr("rect");
  53.  
  54.     /*
  55.      * draw a rectangle around the points (-0.2, -0.2), (-0.2, 0.2),
  56.      * (0.3, 0.2), and (0.3, -0.2).
  57.      */
  58.     rect(-0.2, -0.2, 0.3, 0.2);
  59.  
  60.     color(BLUE);
  61.  
  62.     /*
  63.      * now we want to draw in the top right corner of the screen,
  64.      * and we want to draw a circular circle so we must make sure
  65.      * our viewport is square (if it isn't we'll get an ellipse),
  66.      * so we calculate the shortest edge and set up a viewport which
  67.      * is in the top right region of the screen, but doesn't necessarilly
  68.      * occupy all the top right corner.
  69.      */
  70.                         /* find smallest edge */
  71.     if (maxx - minx > maxy - miny)
  72.         edgelength = (maxy - miny) / 2;
  73.     else 
  74.         edgelength = (maxx - minx) / 2;
  75.  
  76.                         /* create a square viewport */
  77.  
  78.     viewport((maxx - minx) / 2, (maxx - minx) / 2 + edgelength, (maxy - miny) / 2, (maxy - miny) / 2 + edgelength);
  79.  
  80.     cmov2(-0.9, -0.5);
  81.     charstr("circle");
  82.  
  83.     /*
  84.      * draw a circle of radius 0.4 around the point (0.0, 0.0)
  85.      */
  86.     circ(0.0, 0.0, 0.4);
  87.  
  88.     color(GREEN);
  89.  
  90.     /*
  91.      * bottom left hand corner.
  92.      */
  93.     viewport(minx, (maxx - minx) / 2, miny, (maxy - miny) / 2);
  94.  
  95.     cmov2(-0.9, -0.5);
  96.     charstr("ellipse");
  97.  
  98.     /*
  99.      * To draw an ellipse we change the aspect ratio so it is no longer
  100.      * 1 and call circ. In this case we use ortho2 to make the square
  101.      * viewport appear to be higher than it is wide. Alternatively you
  102.      * could use arc to construct one.
  103.      *
  104.      * The call to pushmatrix saves the current viewing transformation.
  105.      * After the ortho2 has been done, we restore the current viewing
  106.      * transformation with a call to popmatrix. (Otherwise everything
  107.      * after the call to ortho would come out looking squashed as the
  108.      * world aspect ratio is no longer 1).
  109.      */
  110.     pushmatrix();
  111.         ortho2(-1.0, 1.0, -1.0, 2.0);
  112.         circ(0.0, 0.5, 0.4);
  113.     popmatrix();
  114.  
  115.     color(RED);
  116.  
  117.     /*
  118.      * bottom right hand corner
  119.      */
  120.     viewport((maxx - minx) / 2, maxx, miny, (maxy - miny) / 2);
  121.  
  122.     cmov2(-0.9, -0.5);
  123.     charstr("arc");
  124.  
  125.     /*
  126.      * draw an arc centered at (0.0, 0.0), radius of 0.4. 0.0 is the start
  127.      * angle and 90.0 is the end angle of the arc being drawn. So this
  128.      * draws a quarter circle - unless our viewport isn't square.
  129.      */
  130.     arc(0.0, 0.0, 0.4, 0, 900);
  131.  
  132.     /*
  133.      * enable the keyboard
  134.      */
  135.     qdevice(KEYBD);
  136.     unqdevice(INPUTCHANGE);
  137.  
  138.     /*
  139.      * wait for the event
  140.      */
  141.     qread(&val);
  142.  
  143.     gexit();
  144. }
  145.